let baseUrl = "http://localhost:8080/carSales";
let token = localStorage.getItem("token");


function selectAjax(type, url, data, result) {
    $.ajax({
        type: type,
        url: baseUrl + url,
        data: data,
        beforeSend: function (request) {
            request.setRequestHeader("Authentication", token);
        },
        dataType: "json",
        success: function (data) {
            if (data.code == '401') {
                alert(data.msg)
                top.location.href = "../login/login.html";
            } else if (data.code == '200') {
                result(data);
            } else if (data.code == '500') {
                result(data);
            }
        }, error: function (error) {
            console.log(error)
        }
    })
}

function addOrUpdateAjax(type, url, data, result) {
    $.ajax({
        type: type,
        url: baseUrl + url,
        data: JSON.stringify(data),
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        beforeSend: function (request) {
            request.setRequestHeader("Authentication", token);
        },
        success: function (data) {
            if (data.code == '401') {
                alert(data.msg)
                top.location.href = "../login/login.html";
            } else if (data.code == '200') {
                result(data);
            } else if (data.code == '500') {
                alert(data.msg)
            }
        },
        error: function (error) {
            console.log(error)
        }
    })
}

function addOrUpdateAjaxNoJson(type, url, data, result) {
    $.ajax({
        type: type,
        url: baseUrl + url,
        data: data,
        dataType: "json",
        beforeSend: function (request) {
            request.setRequestHeader("Authentication", token);
        },
        success: function (data) {
            if (data.code == '401') {
                alert(data.msg)
                top.location.href = "../login/login.html";
            } else if (data.code == '200') {
                result(data);
            } else if (data.code == '500') {
                alert(data.msg)
            }
        },
        error: function (error) {
            console.log(error)
        }
    })
}

function deleteAJAX(type, url, data, result) {
    $.ajax({
        type: type,
        url: baseUrl + url,
        data: data,
        dataType: "json",
        beforeSend: function (request) {
            request.setRequestHeader("Authentication", token);
        },
        success: function (data) {
            if (data.code == '401') {
                alert(data.msg)
                top.location.href = "../login/login.html";
            } else if (data.code == '200') {
                result(data);
            } else if (data.code == '500') {
                alert(data.msg)
            }
        },
        error: function (error) {
            console.log(error)
        }
    })
}

function deleteRow(type, url, data, index, rows) {
    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(() => {
        let _this = this;
        $.ajax({
            url: baseUrl + url,
            type: type,
            data: data,
            headers: {
                "Authentication": token
            },
            dataType: "json",
            success: function (data) {
                if (data == true) {
                    alert("存在使用情况的用品不能删除")
                } else {
                    _this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    rows.splice(index, 1);
                }
            }
        })
    }).catch(() => {
        this.$message({
            type: 'info',
            message: '已取消删除'
        });
    });
}